﻿Function Publish-ToMiddlewareLogs
{
   [cmdletbinding(DefaultParameterSetName="none")]
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [PSObject] 
        $Log
        )
    Process
    {
        if (-not $global:MiddlewareLogs)
        {
            $global:MiddlewareLogs = @()
        }
        $global:MiddlewareLogs += $Log
        $Log.UniqueId
    }

}

Function Reset-MiddlewareLogs
{
   [cmdletbinding(DefaultParameterSetName="none")]
    Param()
    Process
    {
        $global:MiddlewareLogs = @()
    }

}


Function Get-LogMessage
{
    [cmdletbinding(DefaultParameterSetName="none")]
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [System.Management.Automation.CommandInfo] 
        $Command,

        [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [String] 
        $Message,

        [Parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [Validateset("Information","Error","Warning")]
        [String] 
        $Category="Information",

        [Parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [System.Guid] 
        $UniqueId,


        [Parameter(ParameterSetName="AdditionalData", Mandatory=$true,ValueFromPipeline=$false)]
        [Object] 
        $AdditionalData
        )
    Process
    {
        if (-not $UniqueId)
        {
            $UniqueId = [System.Guid]::NewGuid()
        }

        $log = New-Object PSObject -Property @{
                                        Date = (get-date)
                                        Function = $Command.Name
                                        Module = $Command.ModuleName
                                        Version = $Command.Version
                                        UniqueId = $UniqueId
                                        Message = $Message
                                        }

        if ($PSCmdlet.ParameterSetName -eq 'AdditionalData')
        {
            $log | Add-Member -MemberType NoteProperty -Name AdditionalData -Value $AdditionalData
        }

        Write-Verbose ("{0}`t{1}`t{2}`t{3}`t{4}" -f $log.date, $log.category, $log.Module, $log.Function, $log.Message)

        $log
                                                
    }

}

